package org.entities;


import org.GameState;
import org.Progress;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SpriteSheet;



public class Sas extends LevelObject {


    private Animation fallback,sLOpening,sROpening,sRClosing,sLClosing;
    private boolean opened;
    private boolean closed;
    private boolean animLimiter1,animLimiter2;
    private Sound doorOpen;
    private Sound doorClose;
    private boolean noSoundWhileInit,noSoundWhileInit2;

    public Sas(int x, int y, int test[], String type) throws SlickException {

        super(x,y,				
                x+test[0], y+test[1], 
                x+test[2], y+test[3],
                x+test[4], y+test[5],
                x+test[6], y+test[7],
                type);


        SpriteSheet sheet1=new SpriteSheet("data/anim/doors3.png",16,64);
        sLOpening =new Animation();
        for (int frame = 0; frame < 4; frame++) {
            sLOpening .addFrame(sheet1.getSprite(frame,0), 30);
        }
        SpriteSheet sheet4=new SpriteSheet("data/anim/doors3.png",16,64);
        sLClosing=new Animation();
        for (int frame = 4; frame > -1; frame--) {
            sLClosing .addFrame(sheet4.getSprite(frame,0), 30);
        }
        SpriteSheet sheet2=new SpriteSheet("data/anim/doors3.png",16,64);
        sROpening=new Animation();
        for (int frame = 0; frame < 4; frame++) {
            sROpening .addFrame(sheet2.getSprite(frame,1), 30);
        }
        SpriteSheet sheet3=new SpriteSheet("data/anim/doors3.png",16,64);
        sRClosing=new Animation();
        for (int frame = 4; frame > -1; frame--) {
            sRClosing.addFrame(sheet3.getSprite(frame,1), 30);
        }

        sLClosing .setAutoUpdate(false);
        sLOpening .setAutoUpdate(false);
        sRClosing .setAutoUpdate(false);
        sROpening .setAutoUpdate(false);
        sLClosing. setLooping(false);
        sLOpening. setLooping(false);
        sRClosing. setLooping(false);
        sROpening. setLooping(false);
        doorOpen = new Sound("data/sound/Dooropen2.ogg");
        doorClose = new Sound("data/sound/Doorclose2.ogg");
        noSoundWhileInit=true;
        noSoundWhileInit2=true;
    }	

    public void update(int delta) throws SlickException {

        GameState.entityCollisionWith(this);       

        if (opened&&!animLimiter1){
           
            if (type.equals("sasLeft")) {fallback=sLOpening;}
            if (type.equals("sasRight")){fallback=sROpening;}
            opening(delta);
        }

        if (closed&&!animLimiter2){
            
            if (type.equals("sasLeft")) {fallback=sLClosing;}
            if (type.equals("sasRight")){fallback=sRClosing;}
            closing(delta);
        } 
    }


    public void opening(int delta){   
        noSoundWhileInit=false;
        fallback.update(delta);
        if (fallback.isStopped()){
            animLimiter1=true;
            animLimiter2=false;
            if (!noSoundWhileInit2)
            doorOpen.play(1.0f,Progress.sfxVol/20);            
        }        
    }

    public void closing(int delta){
        noSoundWhileInit2=false;
        /*
        sLClosing.restart();
        sLOpening.restart();
        sRClosing.restart();
        sROpening.restart();
        */        
        fallback.update(delta);
        if (fallback.isStopped()){
            animLimiter2=true;
            animLimiter1=false;
            if (!noSoundWhileInit)
            doorClose.play(1.0f,Progress.sfxVol/20);
        }        
    }

    public boolean isCollisionWith(LevelObject other) { 
        if (other.type.equals("samus")){
            if (poly.intersects(other.poly)||poly.contains(other.poly.getX(), other.poly.getY())){
                //System.out.println("touching the sas");
                opened=true;
                closed=false;
            } else {
                opened=false;
                closed=true;                
            }            
        }
        return false;

        //return poly.intersects(other.poly);
    }

    public boolean collidedWith(LevelObject entity) {
        return true;
    }

    public void draw(Graphics g) {
        if (debugMode)
            g.draw(poly);
        if (fallback!=null){            
            if (type.equals("sasLeft")){
                g.drawAnimation(fallback, poly.getX()+40, poly.getY());
            }
            if (type.equals("sasRight")){
                g.drawAnimation(fallback, poly.getX()+24, poly.getY());
            }
        }
    }
}